import java.util.List;

/**
 * @Description
 */

public class MySingleList {

    static class NodeList{
        public int val;
        public NodeList next;

        public NodeList(int val){
            this.val = val;
        }

    }
    public NodeList head;

    NodeList Node1 = new NodeList(1);
    NodeList Node2 = new NodeList(2);
    NodeList Node3 = new NodeList(2);
    NodeList Node4 = new NodeList(4);
    //语句必须在方法中实现
   /* Node1.next = Node2;
    Node2.next = Node3;*/
    public void crateList(){
        Node1.next = Node2;
        Node2.next = Node3;
        Node3.next = Node4;

        this.head = Node1;
    }
    public void show() {
        NodeList cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
    public void addFirst(int data){
        NodeList Node = new NodeList(data);
        Node.next = head;
        head = Node;
    }
    public void addLast(int data){
        NodeList cur = head;
        NodeList Node = new NodeList(data);
        while(cur.next!=null){
            if(head==null){
                head = Node;
            }else{
                cur = cur.next;
            }
        }
        cur.next = Node;
    }
    public int  size(){
        NodeList cur = head;
        int count = 0;
        while(cur!=null){
            count++;
            cur = cur.next;
        }
       return count;
    }

    public void addIndex(int Index,int val){
        NodeList Node = new NodeList(val);
        if(Index<0||Index>size()){
            return;
        } else if (Index==0) {
            addFirst(val);
            return;
        } else if (Index==size()) {
            addLast(val);
            return;
        }else{
            NodeList cur = findList(Index);
            Node.next = cur.next;
            cur.next = Node;
        }
    }
    public NodeList findList(int Index){
        NodeList cur = head;
        int count = 0;
        while(count!=Index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    public void remove(int key){
        if(head==null){
            return;
        }
        if(head.val==key){
            head = head.next;
        }
        NodeList cur = findList(key);
        if(cur==null){
            return;
        }
        NodeList del = cur.next;
        cur.next = del.next;
    }
    public void removeAllKey(int key){
      /*  while(head.next==key){
            head = head.next;
        }*/
        NodeList prev = head;
        NodeList cur = head.next;
        //cur。next！=0的话那么最后一位的时候if语句进不去
        while(cur!=null){
            if(cur.val==key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
        //头部没法处理，最后再删除或者是一开始就遍历，删除所有key
        if(head.val==key){
            head = head.next;
        }
    }

    public NodeList findKey(int key){
        NodeList cur = head;
        while(cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur = cur.next;
        }
        //没有找到
        return null;
    }




}

